

package yf.mall.service.impl;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.common.Const;
import yf.mall.enums.CouponStatusEnum;
import yf.mall.vo.up.CouponUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;
import java.io.InputStream;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  yf.mall.vo.down.CouponDown;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import yf.mall.vo.down.CouponDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import yf.mall.dao.CouponDao;
import yf.mall.entity.CouponEntity;
import yf.mall.vo.CouponVo;
import yf.mall.service.CouponService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date ${datetime}
 */
@Service
public class CouponServiceImpl extends BaseServiceImpl<CouponEntity> implements CouponService {

    private CouponDao dao;


    @Autowired
    public void setDao(CouponDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<CouponVo> findVo(CouponVo vo){
        return dao.findVo(vo);
    }

    @Override
    public CouponVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(CouponVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(CouponVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<CouponVo> list) {
        try {
            List<CouponDown> datas = list.stream().map(item -> new CouponDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), CouponDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, CouponUp.class, new UploadDataListener()).sheet().doRead();
    }

    class UploadDataListener extends AnalysisEventListener<CouponUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(CouponUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }




    /**内部方法**/



    /**外部方法实现**/

    //订单取消
    @Override
    public void cancel(String orderNO) {
        //找到产生的券
        List<CouponEntity> list = dao.findByFromOrderNo(orderNO);
        //删除所有产生的券
        for (int i = 0; i < list.size(); i++) {
            dao.delete(list.get(i).getId());
        }
        //找到使用的券
        list = dao.findByUserOrderNo(orderNO);
        //恢复已经使用的券
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStatus(CouponStatusEnum.NEW.getIndex());
            dao.update(list.get(i));
        }
    }

    @Override
    public List<CouponEntity> findByUserId(Long userId) {
        return dao.findByUserId(userId);
    }

    @Override
    public List<CouponEntity> findByUserIdAct(Long userId) {
        List<CouponEntity> list = findByUserId(userId);
        //七天后的券 未使用的券 未过期的券
        list = list.stream().filter(l->l.isAct()).collect(Collectors.toList());
        return list;
    }
}

